﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using NK.Domain.Abstract;
using NK.Domain.Entities;
using NK.WebUI.Controllers;
using NK.WebUI.HtmlHelpers;
using NK.WebUI.Models;

namespace NK.UnitTests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void CanPaginate()
        {
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(m => m.Products).Returns(new List<Product>
                                                    {
                                                        new Product {ProductID = 1, Name = "P1", Price = 25},
                                                        new Product {ProductID = 2, Name = "P2", Price = 179},
                                                        new Product {ProductID = 3, Name = "P3", Price = 32},
                                                        new Product {ProductID = 4, Name = "P4", Price = 43},
                                                        new Product {ProductID = 5, Name = "P5", Price = 95}
                                                    }.AsQueryable());
            //create a controller and make the page size 3 items
            ProductController controller = new ProductController(mock.Object);
            controller.PageSize = 3;

            //Action
            IEnumerable<Product> result = (IEnumerable<Product>) controller.List(2).Model;

            //Assert
            Product[] prodArray = result.ToArray();
            Assert.IsTrue(prodArray.Length==2);
            Assert.AreEqual(prodArray[0].Name,"P4");
            Assert.AreEqual(prodArray[1].Name, "P5");
        }

        [TestMethod]
        public void CanGeneratePageLinks()
        {
            HtmlHelper myHelper = null;

            PagingInfo pagingInfo = new PagingInfo
                                        {
                                            CurrentPage = 2,
                                            TotalItems = 28,
                                            ItemsPerPage = 10
                                        };
            Func<int, string> pageUrlDelegate = i => "Page" + i;

            MvcHtmlString result = myHelper.PageLinks(pagingInfo, pageUrlDelegate);

            //Assert
            Assert.AreEqual(result.ToString(), @"<a href=""Page1"">1</a><a class=""selected"" href=""Page2"">2</a><a href=""Page3"">3</a>");
        }
    }
}
